﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace CLVisionEICS
{
    [Serializable]
    internal class TreeItemStruct
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                //判断是否更改名称，若更改  移除旧名称
                if (OriginalName != null && Parent != null)
                {
                    Parent.NamesHashSet.Remove(OriginalName);
                    //判断新名称是否重名
                    if (!Parent.NamesHashSet.Add(value))
                    {
                        Parent.NamesHashSet.Add(OriginalName);
                        return;
                    }
                }
                _name = value;
            }
        }

        public HashSet<string> NamesHashSet { get; set; }

        public string ImagePath { get; set; }

        public TreeItemStruct Parent { get; set; }

        public int ArrayIndex { get; set; }

        public bool IsArray { get; set; }

        private DataGridStruct _DataGridStructOfTreeItem;
        public DataGridStruct DataGridStructOfTreeItem
        {
            get { return _DataGridStructOfTreeItem; }
            set
            {
                _DataGridStructOfTreeItem = value;
            }
        }



        public string OriginalName { get; set; }
        private bool _isRename;
        public bool IsRename
        {
            get { return _isRename; }
            set
            {

                if (_isRename != value)
                {
                    if (value)
                    {
                        OriginalName = Name;
                    }
                    _isRename = value;
                }

            }
        }


        public ObservableCollection<TreeItemStruct> Children { get; set; }


        public TreeItemStruct()
        {

            Children = new ObservableCollection<TreeItemStruct>();
            NamesHashSet = new HashSet<string>();
            DataGridStructOfTreeItem = new DataGridStruct();

            Children.CollectionChanged += (sender, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (TreeItemStruct newItem in e.NewItems)
                    {
                        string tempName = newItem.Name.ToString();
                        while (true)
                        {
                            if (NamesHashSet.Add(tempName))
                            {
                                newItem.Name = tempName;
                                break;
                            }
                            tempName = tempName + "_copy";
                        }
                    }
                }
            };
        }

        public TreeItemStruct Clone()
        {
            // 创建一个新的 TreeItemStruct 对象
            var cloned = new TreeItemStruct
            {
                Name = this.Name,
                ImagePath = this.ImagePath,
                Parent = null, // 避免循环引用
                ArrayIndex = this.ArrayIndex,
                IsArray = this.IsArray,
                NamesHashSet = new HashSet<string>(this.NamesHashSet)
            };

            // 递归克隆 DataGridStructOfTreeItem 属性
            if (this.DataGridStructOfTreeItem != null)
            {
                cloned.DataGridStructOfTreeItem = this.DataGridStructOfTreeItem.Clone();
            }

            // 递归克隆 Children 集合
            cloned.Children = new ObservableCollection<TreeItemStruct>();
            foreach (var child in this.Children)
            {
                var clonedChild = child.Clone();
                clonedChild.Parent = cloned;
                cloned.Children.Add(clonedChild);
            }

            return cloned;
        }

    }

}
